import { NodeStatus } from "../../../common/Enum";
import BTComposite from "../../Base/BTComposite";
/** 随机选择节点，其中一个子节点返回false就返回false，所有子节点返回true就返回true */
export default class CompositeRandomSequence extends BTComposite {

    executionOrder: Array<number> = []

    get index() {

        return this.executionOrder[this.executionOrder.length - 1]
    }

    onStart(): void {
        super.onStart()
        this.shuffle()
    }

    shuffle() {
        this.executionOrder = []
        const indexList = Array.from({ length: this.children.length }, (e, i) => i)

        for (let i = indexList.length - 1; i >= 0; i--) {
            const randomIndex = Math.floor(Math.random() * indexList.length)
            this.executionOrder.push(indexList.splice(randomIndex, 1)[0])
        }
    }



    canExecute(): boolean {
        return this.executionOrder.length && this.status !== NodeStatus.Failure
    }

    onChildExecuted(status: NodeStatus): void {
        switch (status) {
            case NodeStatus.Success:
                this.executionOrder.pop()
                if (!this.executionOrder.length) {
                    this.status = NodeStatus.Success
                } else {
                    this.status = NodeStatus.Running
                }
                break;
            case NodeStatus.Failure:
                this.status = NodeStatus.Failure
                break;
            case NodeStatus.Running:
                this.status = NodeStatus.Running
                break;
            default:
                break;
        }
    }

    onConditionalAbort(index: number) {
        this.status = NodeStatus.Inactive
        this.shuffle()
    }
    // onUpdate(): NodeStatus {

    //     if (this.status === NodeStatus.Failure) {
    //         return NodeStatus.Failure
    //     }
    //     if (!this.executionOrder.length) {
    //         return NodeStatus.Success
    //     }
    //     const child = this.children[this.index]
    //     const res = child.run()

    //     if (res === NodeStatus.Failure) {
    //         this.status = NodeStatus.Failure
    //         return NodeStatus.Failure
    //     }

    //     if (res === NodeStatus.Success) {
    //         this.executionOrder.pop()
    //     }

    //     return NodeStatus.Running
    // }
}